package com.simple.demo.io;

import java.io.*;

/**
 * 字符流
 */
public class IOTest {

    public static void main(String[] args)  {

        t4();


    }


    private static void t4(){
        Reader reader = null;

        Writer writer = null;

        File fw = new File("D:\\tmp\\0601\\out1.txt");
        File fr = new File("D:\\tmp\\0601\\out.txt");
        try {
            reader = new FileReader(fr);
            writer = new FileWriter(fw);
            char [] bytes = new char[(int) fr.length()];
            int size = reader.read(bytes);
            writer.write(bytes);
            writer.flush();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    private static void t3(){
        BufferedWriter out = null;


        BufferedReader br = null;

        try {
            br = new BufferedReader(new FileReader("D:\\tmp\\0601\\out.txt"));
            out = new BufferedWriter(new FileWriter("D:\\tmp\\0601\\end.txt"));
            String line = null;
            while ((line = br.readLine())!= null ){
                out.write(line);
                out.flush();
                out.newLine();

            }

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally{
            if(out != null){
                try {
                    out.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(br != null){
                try {
                    br.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }


    private static void t2(){
        BufferedWriter out = null;

        BufferedReader br = null;


        try {
            out = new BufferedWriter( new FileWriter("D:\\tmp\\0601\\out.txt"));

            br = new BufferedReader(new InputStreamReader(System.in));
            String line = null;

            while ((line = br.readLine())!= null ){
                out.write(line);
                out.flush();
                out.newLine();
                if(line.equals("exit")){
                    System.exit(1);
                }

            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally{
            if(out != null){
                try {
                    out.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(br != null){
                try {
                    br.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

    }

    private static void t1(){
        PrintWriter out = null;
        BufferedReader br = null;
        try{
            System.out.println("请输入:");
            out = new PrintWriter(System.out,true);
            br = new BufferedReader(new InputStreamReader(System.in));
            String line = null;
            while((line = br.readLine())!= null){
                if(line.equals("exit")){
                    System.exit(1);
                }
                out.println(line);
            }
        }catch(Exception c){
            c.printStackTrace();
        }finally{
            if(out != null){
                out.close();
            }
            if(br != null){
                try {
                    br.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
